package com.eliauk.democommon.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.util.Arrays;

@Data
public class PageParamDto<E> {

    private int currPage;   // 当前页码
    private int pageSize;   // 每页大小
    private String asc;  // 升序排序字段，逗号分隔
    private String desc; // 降序排序字段，逗号分隔


    public PageParamDto(int page, int size, String asc, String desc) {
        this.currPage = page;
        this.pageSize = size;
        this.asc = asc;
        this.desc = desc;
    }
    public QueryWrapper<E> getQueryWrapper(){
        QueryWrapper<E> queryWrapper = new QueryWrapper<>();
        //升序
        String asc = this.getAsc();
        if (!StrUtil.isEmpty(asc) && !"null".equals(asc)) {
            String[] split = asc.split(",");
            queryWrapper.orderByAsc(Arrays.asList(split));
        }
        //降序
        String desc = this.getDesc();
        if (!StrUtil.isEmpty(desc) && !"null".equals(desc)) {
            String[] split = desc.split(",");
            queryWrapper.orderByDesc(Arrays.asList(split));
        }
        return queryWrapper;
    }

    public Page<E> getPage(){
        //限制条件
        if (this.getCurrPage() < 1) {
            this.setCurrPage(1);
        }
        if (this.getPageSize() > 100) {
            this.setPageSize(100);
        }
        Page<E> page = Page.of(this.currPage, this.pageSize);

        return page;
    }
}
